<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/12/5
 * Time: 13:44
 */

namespace app\admin\controller;


use houdunwang\arr\Arr;
use think\Controller;

/**
 * Class Rules
 * @package app\admin\controller
 * 权限管理之规则管理看控制器
 */
class Rule extends Controller
{
    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Rule();
    }

    //主页
    public function index(){
        //2分配数据
        $data = db('auth_rule')->select();
        $treeData = Arr::tree($data,'title','id','pid');
        //1视图
        return view('',compact('treeData'));
    }
    //添加
    public function add(){

        if(request()->isPost()){
            $data = input('post.');
            $res = $this->db->add($data);
            if ($res['valid']) {
                $this->success($res['msg'], 'index');
            } else {
                $this->error($res['msg']);
            }
        }
        //处理父级规则
        $data = db('auth_rule')->select();
        $treeData = Arr::tree($data,'title','id','pid');
        //1视图
        return view('',compact('treeData'));
    }

    //编辑
    public function edit()
    {
        $id = input('param.id');
        //获取旧数据
        $oldData = db('auth_rule')->find($id);
        $this->assign('oldData', $oldData);

        //获取除去自己和自己子集的数据
        $field = $this->db->getFieldData($id);
        $this->assign('field', $field);

        if (request()->isPost()) {
            $data = input('post.');
            $res = $this->db->edit($data);
            if ($res['valid']) {
                return $this->success($res['msg'], 'index');
            } else {
                return $this->error($res['msg']);
            }
        }

        return view();
    }
    //删除
    public function del(){
        $id = input('param.id');
        $res = $this->db->del($id);
        if ($res['valid']){
            $this->success($res['msg'],'index');
        }else{
            $this->error($res['msg']);
        }
    }
}